package com.link.base.base.questionnairesurvey.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.questionnairesurvey.model.Survey;
import com.link.base.base.questionnairesurvey.service.AnswerService;
import com.link.base.base.questionnairesurvey.service.SurveyService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * chan
 *
 * @author 刘鲁
 * @date 2018-07-04
 * @comments 问卷信息
 */
@Controller
@RequestMapping("/link/survey")
public class SurveyController extends BasicController<Survey> {

    @Resource
    private SurveyService surveyService;
    @Resource
    private AnswerService answerService;

    @Override
    public BasicService<Survey> getBasicService() throws Exception {
        return surveyService;
    }

    /**
     * 查询一张问卷详情
     */
    @RequestMapping(value = "/queryOneDetails")
    @ResponseBody
    public Map<String, Object> queryOneDetails(@JsonParam QueryParams qps, @JsonParam Survey entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            Survey survey = surveyService.queryOneDetails(entity);
            result.put("newRow", survey);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 保存问卷
     */
    @RequestMapping(value = "/saveSurvey")
    @ResponseBody
    public Map<String, Object> saveSurvey(@RequestBody Survey entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            surveyService.SaveSurvey(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 提交问卷
     */
    @RequestMapping(value = "/submitSurvey")
    @ResponseBody
    public Map<String, Object> submitSurvey(@JsonParam Survey entity, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            surveyService.submitSurvey(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 发布问卷
     */
    @RequestMapping(value = "/issueSurvey")
    @ResponseBody
    public Map<String, Object> issueSurvey(@RequestBody Survey entity, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            surveyService.issueSurvey(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 保存问卷答案，不校验
     *
     * @author NingLanhao
     * @date 2019-06-05 09:41
     */
    @RequestMapping(value = "/saveSurveyAnswer")
    @ResponseBody
    public Map<String, Object> saveSurveyAnswer(@JsonParam Survey entity, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            surveyService.saveSurveyAnswer(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 提交问卷
     */
    @RequestMapping(value = "/submitSurveyByH5")
    @ResponseBody
    public Map<String, Object> submitSurveyByH5(@JsonParam Survey entity, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            surveyService.submitSurvey(entity);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 结果统计
     */
    @RequestMapping(value = "/queryCountBySurvey")
    @ResponseBody
    public Map<String, Object> queryCountBySurvey(@JsonParam QueryParams qps, @JsonParam Survey entity,
                                                  HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            qps.preDealSecurity(request);
            Survey temp = (Survey) BasicModel.transformClass(entity, qps);
            this.beforQueryAllOrExample(temp, request);
            this.beforQueryAll(temp, request);
            Map<String, Map<String, Object>> rows = answerService.queryAnsBySurveyId(entity);

            result.put("success", true);
            result.put("rows", rows);
        } catch (BasicServiceException var9) {
            var9.printStackTrace();
            LCLogger.withException(var9);
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", var9.getMessage());
            result.put("detailMessage", var9.getDetailMessage());
        } catch (Exception var10) {
            var10.printStackTrace();
            LCLogger.withException(var10);
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", var10.getMessage());
        }

        return result;
    }

    /**
     * 预处理：获取当前登录人id
     */
    @Override
    public void beforQueryAllOrExample(Survey entity, HttpServletRequest request) throws Exception {
        CoreUser user = UserUtil.getUser();
        entity.setLoginUserId(user.getId().toString());
    }

    /**
     * 生成问卷小程序二维码
     * @return Map
     */
    @RequestMapping(value = "/createMiniprogramCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> createMiniprogramCode(Survey survey) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            result = surveyService.createMiniprogramCode(survey);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询一张问卷详情
     * H5页面使用
     */
    @RequestMapping(value = "/queryOneDetailsByH5")
    @ResponseBody
    public Map<String, Object> queryOneDetailsByH5(@JsonParam QueryParams qps, @JsonParam Survey entity, HttpSession session,
                                                   HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            Survey survey = surveyService.queryOneDetails(entity);
            result.put("newRow", survey);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 复制一张问卷
     */
    @RequestMapping(value = "/copyFromTemplate")
    @ResponseBody
    public Map<String, Object> copyFromTemplate(@RequestBody Survey entity, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap(8);
        try {
            Survey survey = surveyService.copyFromTemplate(entity);
            result.put("newRow", survey);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 替换跳题id
     * @param entity
     * @return
     */
    @RequestMapping(value = "/replaceNextTopicId")
    @ResponseBody
    public Map<String, Object> replaceNextTopicId(@RequestBody Survey entity) {
        Map<String, Object> result = new HashMap(8);
        try {
            Survey survey = surveyService.replaceNextTopicId(entity);
            result.put("result", survey);
            result.put("success", true);
        }  catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
